import 'package:shared_preferences/shared_preferences.dart';

class SoldCache {
  SoldCache._();
  static final SoldCache instance = SoldCache._();

  static const String _key = 'sold_goods_ids';
  Set<int> _ids = <int>{};
  bool _loaded = false;

  Future<void> _ensureLoaded() async {
    if (_loaded) return;
    final prefs = await SharedPreferences.getInstance();
    final list = prefs.getStringList(_key) ?? <String>[];
    _ids = list.map((e) => int.tryParse(e) ?? -1).where((e) => e > 0).toSet();
    _loaded = true;
  }

  Future<Set<int>> getIds() async {
    await _ensureLoaded();
    return _ids;
  }

  Future<void> add(int goodsId) async {
    await _ensureLoaded();
    _ids.add(goodsId);
    final prefs = await SharedPreferences.getInstance();
    await prefs.setStringList(_key, _ids.map((e) => e.toString()).toList());
  }
}


